#include <stdio.h>
#include <time.h>

#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;

string cardArray[] = {
    "A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", 
    "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", 
    "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦"
    "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣", 
    // , "Joker1", "Joker2"
};
int cardArrayLength = sizeof(cardArray) / sizeof(cardArray[0]);

// 从扑克牌中随机抽取n张牌，放入数组中
void getCard(vector<string> &card, int n) {
    vector flag(cardArrayLength, 0);
    srand(time(NULL));
    for (int i = 0; i < n; i++) {
        int t;
        do {
            t = rand() % cardArrayLength;
        } while (flag[t]);
        flag[t] = 1;
        card.push_back(cardArray[t]);
    }
}

// 扑克牌撕两半，合并在一起
void tearCard(vector<string> &card, int n) {
    for (int i = 0; i < n; i++) {
        string s  = card[i];
        card[i] += "-1";
        card.push_back(s + "-2");
    }
}

// 从前面拿n个元素放到中间第m个元素后面
void move(vector<string> &card, int n, int m) {
    vector<string> temp(n);
    // 保存前n个元素
    for (int i = 0; i < n; i++) {
        temp[i] = card[i];
    }
    // 移动m-n个元素
    int ml = m - n;
    for (int i = 0; i < ml; i++) {
        card[i] = card[i + n];
    }
    // 把前n个元素放到m后面
    for (int i = 0; i < n; i++) {
        card[i + ml] = temp[i];
    }
}

// 拿掉第一个张牌
void deleteFirst(vector<string> &card) {
    // 删除第一个元素
    card.erase(card.begin());
}

// 打印扑克牌
void printCard(vector<string> &card) {
    for (string &s : card) {
        cout << s << " ";
    }
    cout << endl;
}

void printQueue(deque<string> &q) {
    for (auto e : q) {
        cout << e << " ";
    }
    cout << endl;
}

int main() {
    // 保存8张牌
    vector<string> card;

    printf("抽取4张牌：\n");
    getCard(card, 4);
    printCard(card);
    printf("撕两半，合并在一起：\n");
    tearCard(card, 4);
    printCard(card);

    printf("按照姓名字数拿牌，你的姓名有几个字：");
    int n, m;
    scanf("%d", &n);
    printf("把%d张牌放到最后面：", n);
    move(card, n, 8);
    printCard(card);

    printf("最上面的3张扑克牌插到中间，输入插入的位置[4-7]：");
    scanf("%d", &m);
    printf("上面的3张扑克牌插到第%d张的后面：", m);
    move(card, 3, m);
    printCard(card);

    // 把第一张牌藏起来
    string first = card[0];
    deleteFirst(card);
    printf("把第一张牌 %s 藏起来：", first.c_str());
    printCard(card);

    // 根据地域分布拿牌
    printf("你的地域是：南方输入1，北方输入2，南北方输入3：");
    scanf("%d", &n);
    printf("插到第几张的后面[4-6]：");
    scanf("%d", &m);
    move(card, n, m);
    printCard(card);

    // 男生仍1张，女生仍2张
    printf("你的性别是：男生输入1，女生输入2：");
    scanf("%d", &n);
    int left = 7 - n;
    deleteFirst(card);
    if (n == 2) {
        deleteFirst(card);
    }
    printCard(card);

    // 见证奇迹的时刻
    printf("见证奇迹的时刻\n");
    for (int i = 1; i <= 7; i++) {
        printf("%d：", i);
        move(card, 1, left);
        printCard(card);
    }

    // 好运留下来，烦恼丢出去
    deque<string> q;
    for (int i = 0; i < left; i++) {
        q.push_back(card[i]);
    }

    printf("好运留下来，烦恼丢出去：\n");
    n = 0;
    while (q.size() > 1) {
        string e;
        printf("第%d次\n", ++n);
        printf("好运留下来\n");
        e = q.front();
        q.pop_front();
        q.push_back(e);
        printf("烦恼丢出去\n");
        e = q.front();
        q.pop_front();
        printQueue(q);
    }
    cout << "最后留下来的牌是：" << q.front() << ", 藏起来的牌是：" << first << endl;
    return 0;
}